﻿using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.Odbc;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class public_html_NewSiteTest_Releaseliste : System.Web.UI.Page
{
    public string SpilID = "";
    public string Format = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        DateFromMySql NewDate = new DateFromMySql();
        string Nyhed = Request.QueryString["id"].ToString();
        ReviewerMySql SQL = new ReviewerMySql();
        OdbcDataReader MainReader = SQL.ExecuteReader("SELECT V2_Nyhed.*, V2_Writters.navn FROM V2_Nyhed JOIN V2_Writters ON V2_Writters.id = V2_Nyhed.Forfatter WHERE V2_Nyhed.id = " + Nyhed);
        MainReader.Read();
        lblTitel.Text = MainReader["Titel"].ToString();
        lblHeader.Text = MainReader["Header"].ToString();
        lblTekst.Text = Regex.Replace(MainReader["Tekst"].ToString(), Environment.NewLine, "<br />");
        lblForfatter.Text = MainReader["navn"].ToString();
        lblEmne.Text = MainReader["Emne"].ToString();
        lblDato.Text = "Lagt online d. " + NewDate.GetDate(MainReader["Dato"].ToString());

        if (MainReader["Kilde"].ToString() != "")
        {
            lblKilde.Text = "Kilde: " + MainReader["Kilde"].ToString();
        }

        if (File.Exists(Server.MapPath("news/" + MainReader["NewsID"].ToString() + ".jpg")))
        {
            imgCover.ImageUrl = "news/" + MainReader["NewsID"].ToString() + ".jpg";
            imgCover.Width = 200;
        }
        else
        {
            imgCover.Visible = false;
        }

        string RelSpilID = "";

        int x = 0;
        int Relcounter = 0;
        string TitelHolder = "";
        OdbcDataReader RelReader = SQL.ExecuteReader("SELECT V2_NewsRelations.*, V2_Spil.Titel, V2_Spil.id AS RealSpilID FROM V2_NewsRelations JOIN V2_Spil ON V2_NewsRelations.SpilID = V2_Spil.SpilID WHERE NewsID = '" + MainReader["NewsID"].ToString() + "'");
        DivRelSpil.InnerHtml = "<table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\" class=\"infotable\">";
        while (RelReader.Read())
        {
            if (RelReader["Titel"].ToString() != TitelHolder)
            {
                DivRelSpil.InnerHtml += "<tr><td class=\"TD_Top10\"><a style=\"color:black;\" href=\"" + UI.LinkMake.LinkSpil(RelReader["RealSpilID"].ToString(), RelReader["Titel"].ToString()) + "\">" + RelReader["Titel"].ToString() + "</a></td></tr>";
                x = 0;
                RelSpilID = RelReader["SpilID"].ToString();
            }
            TitelHolder = RelReader["Titel"].ToString();
            Relcounter++;
        }
        DivRelSpil.InnerHtml += "</table>";
        if (Relcounter == 0)
        {
            DivRelSpil.Visible = false;
            DivRelSpilHeader.Visible = false;
        }
        
        if (MainReader["Screens"].ToString() == "True")
        {
            OdbcDataReader ScreenReader = SQL.ExecuteReader("SELECT * FROM V2_Screenshots WHERE SpilID = '" + RelSpilID + "' GROUP BY Filnavn");
            if (ScreenReader.Read())
            {
                imgScreen1.ImageUrl = "screenshots/" + ScreenReader["Filnavn"].ToString();
                HyperLink1.NavigateUrl = "Screenshot.aspx?PicID=" + ScreenReader["ID"].ToString() + "&SpilID=" + RelSpilID;
            }
            if (ScreenReader.Read())
            {
                imgScreen2.ImageUrl = "screenshots/" + ScreenReader["Filnavn"].ToString();
                HyperLink2.NavigateUrl = "Screenshot.aspx?PicID=" + ScreenReader["ID"].ToString() + "&SpilID=" + RelSpilID;
            }
            if (ScreenReader.Read())
            {
                imgScreen3.ImageUrl = "screenshots/" + ScreenReader["Filnavn"].ToString();
                HyperLink3.NavigateUrl = "Screenshot.aspx?PicID=" + ScreenReader["ID"].ToString() + "&SpilID=" + RelSpilID;
            }
            if (ScreenReader.Read())
            {
                imgScreen4.ImageUrl = "screenshots/" + ScreenReader["Filnavn"].ToString();
                HyperLink4.NavigateUrl = "Screenshot.aspx?PicID=" + ScreenReader["ID"].ToString() + "&SpilID=" + RelSpilID;
            }
            if (ScreenReader.Read())
            {
                lnkScreens.NavigateUrl = "screenshot.aspx?PicID=" + ScreenReader["ID"].ToString() + "&SpilID=" + RelSpilID;
            }
        }
        else
        {
            DivScreens.Visible = false;
        }
        HtmlMeta HeaderDescription = new HtmlMeta();
        HeaderDescription.Name = "description";
        HeaderDescription.Content = lblHeader.Text.ToString() + "...";
        this.Header.Controls.Add(HeaderDescription);
        this.Title = "Nyhed - " + MainReader["Titel"].ToString();
    }
}
